﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;

public partial class ContentEditor : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
            LoadEditor();
    }

    protected void reSiteWorxEditor_PreRender(object sender, EventArgs e)
    {
        if (reSiteWorxEditor.Links.Count <= 0)
            reSiteWorxEditor.Links.AddRange(Utility.LoadCustomLinks());
    }

    private void LoadEditor()
    {
        string contentId = Request.QueryString["contentId"].ToString();
        string editorType = Request.QueryString["EditorType"].ToString();
        string editorCssClass = Request.QueryString["cssClass"].ToString();

        hidContentCssClass.Value = editorCssClass;

        if (Session[contentId] != null)
            reSiteWorxEditor.Content = Session[contentId].ToString();
        
        string toolsFile = "";

        switch (editorType)
        {
            case "CompletePageEditor":
                toolsFile = "SiteWorx/Webparts/Basic/EditorToolFiles/CompletePageEditorToolsFile.xml";
                break;
            case "DocumentsAndLinks":
                toolsFile = "SiteWorx/Webparts/Basic/EditorToolFiles/DocumentLinksToolsFile.xml";
                break;
            case "Image":
                toolsFile = "SiteWorx/Webparts/Basic/EditorToolFiles/ImageToolsFile.xml";
                break;
            case "Links":
                toolsFile = "SiteWorx/Webparts/Basic/EditorToolFiles/LinksToolsFile.xml";
                break;
            case "Multimedia":
                toolsFile = "SiteWorx/Webparts/Basic/EditorToolFiles/MultimediaToolsFile.xml";
                break;
            case "TextAndImages":
                toolsFile = "SiteWorx/Webparts/Basic/EditorToolFiles/TextAndImagesToolsFile.xml";
                break;

        }
        reSiteWorxEditor.ToolsFile = toolsFile;

        if (ConfigurationManager.AppSettings["ContentEditorHeight"] != null && ConfigurationManager.AppSettings["ContentEditorHeight"].ToString().Length > 0)
            reSiteWorxEditor.Height = Unit.Pixel(int.Parse(ConfigurationManager.AppSettings["ContentEditorHeight"].ToString()));
        else
            reSiteWorxEditor.Height = Unit.Pixel(480);

        if (ConfigurationManager.AppSettings["MaxFlashSize"] != null && ConfigurationManager.AppSettings["MaxFlashSize"].ToString().Length > 0)
            reSiteWorxEditor.FlashManager.MaxUploadFileSize = int.Parse(ConfigurationManager.AppSettings["MaxFlashSize"].ToString());

        if (ConfigurationManager.AppSettings["MaxImageSize"] != null && ConfigurationManager.AppSettings["MaxImageSize"].ToString().Length > 0)
            reSiteWorxEditor.ImageManager.MaxUploadFileSize = int.Parse(ConfigurationManager.AppSettings["MaxImageSize"].ToString());

        if (ConfigurationManager.AppSettings["MaxDocumentSize"] != null && ConfigurationManager.AppSettings["MaxDocumentSize"].ToString().Length > 0)
            reSiteWorxEditor.DocumentManager.MaxUploadFileSize = int.Parse(ConfigurationManager.AppSettings["MaxDocumentSize"].ToString());

        if (ConfigurationManager.AppSettings["MaxMediaSize"] != null && ConfigurationManager.AppSettings["MaxMediaSize"].ToString().Length > 0)
            reSiteWorxEditor.MediaManager.MaxUploadFileSize = int.Parse(ConfigurationManager.AppSettings["MaxMediaSize"].ToString());

        if (ConfigurationManager.AppSettings["MaxTemplateSize"] != null && ConfigurationManager.AppSettings["MaxTemplateSize"].ToString().Length > 0)
            reSiteWorxEditor.TemplateManager.MaxUploadFileSize = int.Parse(ConfigurationManager.AppSettings["MaxTemplateSize"].ToString());
       
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        string contentId = Request.QueryString["contentId"].ToString();
        Session[contentId] = reSiteWorxEditor.Content;
        BuildUpdateScript(contentId);
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        string contentId = Request.QueryString["contentId"].ToString();
        BuildUpdateScript(contentId);
    }

    private void BuildUpdateScript(string contentId)
    {
        string functionName = "Update_" + contentId.Replace("-", "_");
        string script = "var wnd = getRadWindow(); wnd.BrowserWindow." + functionName + "(wnd); wnd.close();";

        ScriptManager.RegisterStartupScript(this, this.GetType(), "UpdateParentPage", script, true);
    }
}
